package org.jeecg.modules.act.wx;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.StringUtils;
import org.jeecg.common.wx.BASE64DecodedMultipartFile;
import org.jeecg.common.wx.UploadUtil;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件上传
 */
@Api(tags = "wx_文件上传")
@RestController
@RequestMapping("/wx/")
public class UploadController {


    @PostMapping("upload")
    @ApiOperation(value = "文件上传", notes = "文件上传")
    public Result upload(@RequestParam("file") MultipartFile file) {
//        String path = "d://profile//";
        //服务器地址
        String path = "/files/";
        return UploadUtil.upload(file, path);
    }


    @PostMapping("uploadByBase64")
    @ApiOperation(value = "文件上传base64", notes = "文件上传base64")
    public Result uploadByBase64(String fileBase64) {
        Result result = new Result();
        if (StringUtils.isEmpty(fileBase64)) {
            result.setCode(1);
            result.setMessage("上传失败");
            return result;
        }
//        String path = "d://profile//";
        MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(fileBase64);
        if (file == null) {
            result.setCode(1);
            result.setMessage("上传失败");
            return result;
        }
        //服务器地址
        String path = "/files/";
        return UploadUtil.upload(file, path);
    }
}
